import {Injectable} from "@angular/core";
import {AlertController, ToastController} from "ionic-angular";

@Injectable()
export class AppPopup {
    constructor(private alertCtrl: AlertController, private toastCtrl: ToastController,) {
    }

    confirm(str: string = '您确定此操作吗？', okStr: string = '确定', noStr: string = '取消'): Promise<any> {
        return new Promise((resolve, reject) => {
            return this.alertCtrl.create({
                title: "提示", message: str, enableBackdropDismiss: false, buttons: [{
                    text: okStr, handler: resolve
                }, {
                    text: noStr, handler: () => {
                        reject('操作被取消')
                    }
                }]
            }).present();
        });
    }

    alert(str: string): Promise<any> {
        return new Promise((resolve, reject) => {
            this.alertCtrl.create({
                title: "提示", message: str, enableBackdropDismiss: false, buttons: [{
                    text: '确定', handler: resolve
                }]
            }).present();
        });
    }

    toast(str: string, position: string = 'bottom', duration: number = 3000): void {
        let option = {
            message: str, duration: duration, position: position
        };
        this.toastCtrl.create(option).present();
    }

}
